Add initial support for list slices. This currently allows you to do stuff

like this:

def B {
  list<int> X = [10, 20, 30, 4, 1, 1231, 20] [2-4,2,2,0-6];
}

... which isn't particularly useful, but more is to come.

llvm-svn: 15247
This commit is contained in:
Chris Lattner 2004-07-26 23:21:34 +00:00
parent a559cb8e10
commit 8bf9e06ddd
3 changed files with 65 additions and 16 deletions

View File

@ -300,6 +300,14 @@ Value : INTVAL {
} }
$$ = new DagInit(D, *$3); $$ = new DagInit(D, *$3);
delete $2; delete $3; delete $2; delete $3;
} | Value '[' BitList ']' {
std::reverse($3->begin(), $3->end());
$$ = $1->convertInitListSlice(*$3);
if ($$ == 0) {
err() << "Invalid list slice for value '" << *$1 << "'!\n";
exit(1);
}
delete $3;
}; };
OptVarName : /* empty */ { OptVarName : /* empty */ {
@ -330,41 +338,61 @@ RBitList : INTVAL {
$$ = new std::vector<unsigned>(); $$ = new std::vector<unsigned>();
$$->push_back($1); $$->push_back($1);
} | INTVAL '-' INTVAL { } | INTVAL '-' INTVAL {
if ($1 < $3 || $1 < 0 || $3 < 0) { if ($1 < 0 || $3 < 0) {
err() << "Invalid bit range: " << $1 << "-" << $3 << "!\n"; err() << "Invalid range: " << $1 << "-" << $3 << "!\n";
exit(1); exit(1);
} }
$$ = new std::vector<unsigned>(); $$ = new std::vector<unsigned>();
for (int i = $1; i >= $3; --i) if ($1 < $3) {
$$->push_back(i); for (int i = $1; i <= $3; ++i)
$$->push_back(i);
} else {
for (int i = $1; i >= $3; --i)
$$->push_back(i);
}
} | INTVAL INTVAL { } | INTVAL INTVAL {
$2 = -$2; $2 = -$2;
if ($1 < $2 || $1 < 0 || $2 < 0) { if ($1 < 0 || $2 < 0) {
err() << "Invalid bit range: " << $1 << "-" << $2 << "!\n"; err() << "Invalid range: " << $1 << "-" << $2 << "!\n";
exit(1); exit(1);
} }
$$ = new std::vector<unsigned>(); $$ = new std::vector<unsigned>();
for (int i = $1; i >= $2; --i) if ($1 < $2) {
$$->push_back(i); for (int i = $1; i <= $2; ++i)
$$->push_back(i);
} else {
for (int i = $1; i >= $2; --i)
$$->push_back(i);
}
} | RBitList ',' INTVAL { } | RBitList ',' INTVAL {
($$=$1)->push_back($3); ($$=$1)->push_back($3);
} | RBitList ',' INTVAL '-' INTVAL { } | RBitList ',' INTVAL '-' INTVAL {
if ($3 < $5 || $3 < 0 || $5 < 0) { if ($3 < 0 || $5 < 0) {
err() << "Invalid bit range: " << $3 << "-" << $5 << "!\n"; err() << "Invalid range: " << $3 << "-" << $5 << "!\n";
exit(1); exit(1);
} }
$$ = $1; $$ = $1;
for (int i = $3; i >= $5; --i) if ($3 < $5) {
$$->push_back(i); for (int i = $3; i <= $5; ++i)
$$->push_back(i);
} else {
for (int i = $3; i >= $5; --i)
$$->push_back(i);
}
} | RBitList ',' INTVAL INTVAL { } | RBitList ',' INTVAL INTVAL {
$4 = -$4; $4 = -$4;
if ($3 < $4 || $3 < 0 || $4 < 0) { if ($3 < 0 || $4 < 0) {
err() << "Invalid bit range: " << $3 << "-" << $4 << "!\n"; err() << "Invalid range: " << $3 << "-" << $4 << "!\n";
exit(1); exit(1);
} }
$$ = $1; $$ = $1;
for (int i = $3; i >= $4; --i) if ($3 < $4) {
$$->push_back(i); for (int i = $3; i <= $4; ++i)
$$->push_back(i);
} else {
for (int i = $3; i >= $4; --i)
$$->push_back(i);
}
}; };
BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); }; BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); };

View File

@ -310,6 +310,16 @@ Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
return BI; return BI;
} }
Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
std::vector<Init*> Vals;
for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
if (Elements[i] >= getSize())
return 0;
Vals.push_back(getElement(Elements[i]));
}
return new ListInit(Vals);
}
void ListInit::print(std::ostream &OS) const { void ListInit::print(std::ostream &OS) const {
OS << "["; OS << "[";
for (unsigned i = 0, e = Values.size(); i != e; ++i) { for (unsigned i = 0, e = Values.size(); i != e; ++i) {

View File

@ -304,6 +304,15 @@ struct Init {
return 0; return 0;
} }
/// convertInitListSlice - This method is used to implement the list slice
/// selection operator. Given an initializer, it selects the specified list
/// elements, returning them as a new init of list type. If it is not legal
/// to take a slice of this, return null.
///
virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements) {
return 0;
}
/// getFieldType - This method is used to implement the FieldInit class. /// getFieldType - This method is used to implement the FieldInit class.
/// Implementors of this method should return the type of the named field if /// Implementors of this method should return the type of the named field if
/// they are of record type. /// they are of record type.
@ -466,6 +475,8 @@ public:
return Values[i]; return Values[i];
} }
Init *convertInitListSlice(const std::vector<unsigned> &Elements);
virtual Init *convertInitializerTo(RecTy *Ty) { virtual Init *convertInitializerTo(RecTy *Ty) {
return Ty->convertValue(this); return Ty->convertValue(this);
} }